package com.shababeach.travel.controller;

import com.shababeach.travel.dto.Result;
import com.shababeach.travel.entity.LocationType;
import com.shababeach.travel.service.LocationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 位置类型控制器
 */
@RestController
@RequestMapping("/locationTypes")
public class LocationTypeController {

    @Autowired
    private LocationTypeService locationTypeService;

    /** 分页查询位置类型 */
    @GetMapping("/page")
    public Result<Page<LocationType>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        Pageable pageableRequest = PageRequest.of((current-1), size);

        Page<LocationType> locationTypes = null;
        if (keyword != null && !keyword.isEmpty()) {
            locationTypes = locationTypeService.page(pageableRequest, keyword);
        } else {
            locationTypes = locationTypeService.page(pageableRequest);
        }

        return Result.success(locationTypes);
    }

    /** 获取所有启用的位置类型 */
    @GetMapping("/list")
    public Result<List<LocationType>> list() {
        return Result.success(locationTypeService.getEnabledTypes());
    }

    /** 根据ID获取位置类型 */
    @GetMapping("/{id}")
    public Result<LocationType> getById(@PathVariable Integer id) {
        return Result.success(locationTypeService.getById(id));
    }
} 